//
//  JKREssenceViewController.m
//  百思不得姐
//
//  Created by Lucky on 16/2/29.
//  Copyright © 2016年 Lucky. All rights reserved.
//

#import "JKREssenceViewController.h"
#import "JKRTopicTableViewController.h"

@interface JKREssenceViewController ()<UIScrollViewDelegate>

@property (nonatomic, weak) UIView *titlesView;

@property (nonatomic, weak) UIView *indicatorView;

@property (nonatomic, weak) UIButton *selectedButton;

@property (nonatomic, weak) UIScrollView *contentView;

@end

@implementation JKREssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = JKRGlobalBackgroudColor;
    
    [self setNavigationItem];
    
    [self setupChildViewControllers];
    
    [self setTitlesView];
    
    [self setupContentView];
    
}

- (void)setNavigationItem {
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"MainTagSubIcon"] highLightedImage:[UIImage imageNamed:@"MainTagSubIconClick"] target:self action:@selector(tagClick)];
}

- (void)setupChildViewControllers
{
    
    JKRTopicTableViewController *allTableViewController = [[JKRTopicTableViewController alloc] init];
    allTableViewController.title = @"全部";
    allTableViewController.type = JKRTopicTypeAll;
    
    JKRTopicTableViewController *videoTableViewController = [[JKRTopicTableViewController alloc] init];
    videoTableViewController.title = @"视频";
    videoTableViewController.type = JKRTopicTypeVideo;
    
    JKRTopicTableViewController *voiceTableViewController = [[JKRTopicTableViewController alloc ]init];
    voiceTableViewController.title = @"声音";
    voiceTableViewController.type = JKRTopicTypeVoice;
    
    JKRTopicTableViewController *pictureTableViewController = [[JKRTopicTableViewController alloc] init];
    pictureTableViewController.title = @"图片";
    pictureTableViewController.type = JKRTopicTypePicture;
    
    JKRTopicTableViewController *wordTableViewController = [[JKRTopicTableViewController alloc] init];
    wordTableViewController.title = @"段子";
    wordTableViewController.type = JKRTopicTypeWord;

    
    [self addChildViewController:wordTableViewController];
    [self addChildViewController:allTableViewController];
    [self addChildViewController:videoTableViewController];
    [self addChildViewController:voiceTableViewController];
    [self addChildViewController:pictureTableViewController];
}

- (void)setTitlesView {
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
    titlesView.width = self.view.width;
    titlesView.height = 35;
    titlesView.y = 64;
    [self.view addSubview:titlesView];
    _titlesView = titlesView;
    
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [UIColor redColor];
    indicatorView.height = 2;
    indicatorView.y = titlesView.height - indicatorView.height;
    
    CGFloat width = titlesView.width / self.childViewControllers.count;
    CGFloat height = titlesView.height;
    
    for (NSInteger i = 0; i < self.childViewControllers.count; i++) {
        UIButton *button = [[UIButton alloc] init];
        button.tag = i;
        button.height = height;
        button.width = width;
        button.x = i * width;
        UIViewController *vc = self.childViewControllers[i];
        [button setTitle:vc.title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        [titlesView addSubview:button];
        if (i == 0) {
            [button layoutIfNeeded];
            button.enabled = NO;
            self.selectedButton = button;
            self.indicatorView.width = button.titleLabel.width;
            self.indicatorView.centerX = button.centerX;
        }
    }
    
    [titlesView addSubview:indicatorView];
    _indicatorView = indicatorView;

}

CGFloat const JKREssenceTitleViewMaxY = 99;

- (void)setupContentView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *contentView = [[UIScrollView alloc] init];
    contentView.frame = self.view.bounds;
    contentView.delegate = self;
    contentView.pagingEnabled = YES;
    
    
    
//    CGFloat bottom = self.tabBarController.tabBar.height;
//    CGFloat top = CGRectGetMaxY(self.titlesView.frame);
//    contentView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    [self.view insertSubview:contentView atIndex:0];
    contentView.contentSize = CGSizeMake(self.view.width * self.childViewControllers.count, 0);
    self.contentView = contentView;
    
    [self scrollViewDidEndScrollingAnimation:contentView];
}

- (void)titleClick:(UIButton *)sender {
    
    self.selectedButton.enabled = YES;
    sender.enabled = NO;
    self.selectedButton = sender;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = sender.titleLabel.width;
        self.indicatorView.centerX = sender.centerX;
    }];
    
    CGPoint offset = self.contentView.contentOffset;
    offset.x = sender.tag * self.contentView.width;
    [self.contentView setContentOffset:offset animated:YES];
}

- (void)tagClick
{
    JKRLogFunc;
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    UITableViewController *vc = self.childViewControllers[index];
        
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = 0;
    vc.view.height = scrollView.height;
    [scrollView addSubview:vc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    
    [self titleClick:self.titlesView.subviews[index]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
}


@end
